package com.sxj.shop.controller;

import com.sxj.shop.entity.Item;
import com.sxj.shop.entity.Order;
import com.sxj.shop.entity.Product;
import com.sxj.shop.entity.User;
import com.sxj.shop.exception.PasswordErrorException;
import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserNotLoginException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.CartService;
import com.sxj.shop.service.OrderService;
import com.sxj.shop.service.ProductService;
import com.sxj.shop.service.UserService;
import com.sxj.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/9日 13:33
 * 主页,该页面必须在用户登录后才能访问
 */
public class MainController {

    private User user; // 当前登录进来的用户
    private Scanner sc = new Scanner(System.in);
    // 当用户成功登陆之后,为登陆进来的用户分配一个新的购物车
    // 当前用户购物的商品都会存放到这个购物车中
    private Cart cart = new Cart();
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
    private UserService userService = (UserService) ObjectFactory.getObject("userService");

    public MainController(User user) {
        this.user = user;
    }

    public void showMenu() {
        System.out.println("**************欢迎" + user.getUsername() + "登录网络商城**************");
        System.out.println("请选选择服务(1.加入购物车 2.查看购物车 3.我的订单 4.修改密码 5.退出登录):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            case 3:
                findMyOrders();
                break;
            case 4:
                modifyPassword();
                break;
            case 5:
                logout();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入!");
                showMenu();
                break;
        }
    }

    public void addCart() {
        // 想要将指定商品添加到购物车中,必须先展示出所有的商品
        List<Product> products = productService.findAll();
        if (products.isEmpty()) {
            System.out.println("提示:当前商城暂未营业");
        } else {
            System.out.println("商品编号:\t\t\t\t商品名称\t\t\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t\t\t" + product.getName() + "\t\t\t\t" + product.getPrice());
            }
            System.out.println("--------------------------------------------------------------");
        }
        System.out.print("请输入需要加入购物车中的商品的编号:");
        String id = sc.next();
        try {
            cartService.addCart(cart, id);
            System.out.println("提示:该商品已经成功加入购物车");
            showMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new UserController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        } catch (Exception e) {
            System.out.println("错误:服务器内部异常!");
            showMenu();
        }
    }

    public void showCart() {
        System.out.println("编号\t\t\t\t商品名称\t\t\t\t商品数量\t\t\t\t商品总价");
        List<Item> items = cart.getItems();
        for (Item item : items) {
            System.out.println(item.getProduct().getId() + "\t\t\t\t" + item.getProduct().getName() + "\t\t\t\t" + item.getNum() + "\t\t\t\t" + item.getPrice());
        }
        System.out.println("---------------------------------------");
        System.out.println("购物车总价:" + cart.getPrice());
        System.out.println("---------------------------------------");
        System.out.println("请选择服务(1.删除指定商品 2.修改商品数量 3.清空购物车 4.结算 5.返回主菜单):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                clean();
                break;
            case 4:
                createOrder();
                break;
            case 5:
                showMenu();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入!");
                showCart();
                break;

        }
    }

    public void removeCart() {
        System.out.print("请输入需要删除的商品编号:");
        String id = sc.next();

        try {
            cartService.removeCart(cart, id);
            System.out.println("提示:恭喜您,删除成功!");
            showCart();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new UserController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showCart();
        } catch (Exception e) {
            System.out.println("错误:服务器内部异常!");
            showCart();
        }
    }

    public void modifyCart() {
        System.out.print("请输入需要修改的商品编号:");
        String id = sc.next();
        System.out.print("请输入修改后的数量:");
        String num = sc.next();

        try {
            cartService.modifyCart(cart, id, num);
            System.out.println("提示:恭喜您,修改成功!");
            showCart();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new UserController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showCart();
        } catch (Exception e) {
            System.out.println("错误:服务器内部异常!");
            showCart();
        }
    }

    public void clean() {
        cart = new Cart();
        showCart();
    }

    public void createOrder() {
        try {
            String no = orderService.createOrder(cart, user);
            System.out.println("提示:恭喜您,结算成功,您的订单号为:" + no);
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("错误:服务器内部异常");
        }
        showMenu();
    }

    public void findMyOrders() {
        try {
            List<Order> orders = orderService.findMyOrders(user);
            System.out.println("编号\t\t订单号\t\t总价");
            for (Order order : orders) {
                System.out.println(order.getId() + "\t\t" + order.getNo() + "\t\t" + order.getPrice());
            }
            System.out.print("请选择服务(1.查看订单详情 2.返回主菜单)");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    findOrderDetail();
                    break;
                case 2:
                    showMenu();
                    break;
                default:
                    System.out.println("您输入的选项有误,请重新输入!");
                    break;
            }
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new UserController().showMenu();
        }


    }

    public void findOrderDetail() {
        System.out.print("请输入查询的订单编号:");
        String id = sc.next();
        try {
            Order order = orderService.findDetailById(id);
            System.out.println("用户名:" + order.getUser().getUsername());
            System.out.println("手机号:" + order.getUser().getPhone());
            System.out.println("订单号:" + order.getNo());
            System.out.println("-------------------------------------------");
            System.out.println("序号\t\t商品名称\t\t商品数量\t\t总价");
            List<Item> items = order.getItems();
            for (int i = 0; i < items.size(); i++) {
                Item item = items.get(i);
                System.out.println((i + 1) + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
            }
            System.out.println("-------------------------------------------");
            System.out.println("共:" + order.getPrice() + "元\t\t\t\t总计:" + order.getCount() + "条");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
        } catch (Exception e) {
            System.out.println("错误:服务器内部异常!");
        }
        System.out.print("请选择服务(1.返回我的订单 2.返回主菜单):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                findMyOrders();
                break;
            case 2:
                showMenu();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入!");
                break;
        }
        // 用户名:xxx
        // 手机号:xxx
        // 订单号:xxxx
        // ---------------------------------------
        // 序号   商品名称    商品数量    总价
        // 1        aaa         2           10
        // 2        bbb         5           50
        // 3        ccc         1           20
        // ---------------------------------------
        // 共:80元    总计:3条
    }

    public void modifyPassword() {
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        System.out.print("请输入新密码:");
        String newPassword = sc.next();
        System.out.print("请再次输入新密码:");
        String newPassword2 = sc.next();

        try {
            userService.modifyPassword(user, oldPassword, newPassword, newPassword2);
            System.out.println("提示:恭喜您,密码修改成功!");
            new UserController().showMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        } catch (PasswordErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        } catch (Exception e) {
            System.out.println("错误:服务器内部异常!");
            showMenu();
        }
    }

    public void logout() {
        new UserController().showMenu();
    }

}
